class Manager < ActiveRecord::Base
#unencrypted password
attr_accessor :password

  # validation
  validates_length_of       :email, :within => 3..100
  validates_uniqueness_of   :email, :case_sensitive => false
  validates_presence_of     :name
  validates_length_of       :password, :within => 4..40,
                                       :if => :password_required? 
  
  validates_confirmation_of :password, :if => :password_required?
  
  # callbacks
  before_save :encrypt_password 

  # encrypts given password using salt
  def self.encrypt(pass, salt)
    Digest::SHA1.hexdigest("--#{salt}--#{pass}--")
  end

  protected

  # before save - create salt, encrypt password
  def encrypt_password 
    return if password.blank?
    if new_record?
      self.salt = Digest::SHA1.hexdigest("--#{Time.now}--#{email}--")
    end
    self.encrypted_password = Manager.encrypt(password, salt)  
  end

  # no encrypted password yet OR password attribute is set
  def password_required?
    encrypted_password.blank? || !password.blank?
  end
end
